package com.free.chat.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.solon.service.impl.ServiceImpl;
import com.free.chat.common.enums.MsgSignFlagEnum;
import com.free.chat.dao.ChatMsgDao;
import com.free.chat.entity.ApiResult;
import com.free.chat.entity.dto.ChatMsgDTO;
import com.free.chat.entity.model.ChatMsg;
import com.free.chat.netty.ChatRecord;
import com.free.chat.service.ChatMsgService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.solon.annotation.Db;
import org.noear.solon.aspect.annotation.Service;
import org.noear.solon.core.handle.Context;

import java.util.Date;
import java.util.List;

/**
 * (ChatMsg)表服务实现类
 *
 * @author makejava
 * @since 2022-10-21 20:00:13
 */
@Service
@Slf4j
public class ChatMsgServiceImpl extends ServiceImpl<ChatMsgDao, ChatMsg> implements ChatMsgService {
    @Db
    private ChatMsgDao chatMsgDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ChatMsg queryById(Long id) {
        return this.chatMsgDao.selectById(id);
    }

    /**
     * 新增数据
     *
     * @param chatMsg 实例对象
     * @return 实例对象
     */
    @Override
    public ChatMsg insert(ChatMsg chatMsg) {
        this.chatMsgDao.insert(chatMsg);
        return chatMsg;
    }

    /**
     * 修改数据
     *
     * @param chatMsg 实例对象
     * @return 实例对象
     */
    @Override
    public ChatMsg update(ChatMsg chatMsg) {
        this.chatMsgDao.update(chatMsg, null);
        return this.queryById(chatMsg.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.chatMsgDao.deleteById(id) > 0;
    }

    @Override
    public ApiResult list(ChatMsgDTO chatMsgDTO) {
        ChatMsg chatMsg = new ChatMsg();
        BeanUtil.copyProperties(chatMsgDTO, chatMsg);
        QueryWrapper<ChatMsg> chatMsgQueryWrapper = new QueryWrapper<>();
        if (chatMsgDTO.getHasPage()) {
            PageHelper.startPage(chatMsgDTO.getPageNum(), chatMsgDTO.getPageSize());
            List<ChatMsg> list = chatMsgDao.selectList(chatMsgQueryWrapper);
            PageInfo<ChatMsg> pageInfo = new PageInfo<>(list);
            return ApiResult.success(pageInfo);
        }
        List<ChatMsg> list = chatMsgDao.selectList(chatMsgQueryWrapper);
        return ApiResult.success(list);
    }

    @Override
    public Long saveMsgRecord(ChatRecord chatRecord) {
        ChatMsg chatMsg = BeanUtil.copyProperties(chatRecord, ChatMsg.class);
        chatMsg.setId(IdUtil.getSnowflakeNextId());
        chatMsg.setCreateTime(new Date());
        chatMsg.setSignFlag(MsgSignFlagEnum.un_sign.type);
        chatMsgDao.insert(chatMsg);
        return chatMsg.getId();
    }

    @Override
    public ApiResult getChatMsgByAcceptUserId(ChatMsgDTO chatMsgDTO) {
        PageHelper.startPage(chatMsgDTO.getPageNum(), chatMsgDTO.getPageSize());
        Long userId = (Long) Context.current().session("loginUserId");
        LambdaQueryWrapper<ChatMsg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatMsg::getAcceptUserId, chatMsgDTO.getAcceptUserId())
                .eq(ChatMsg::getSendUserId, userId)
                .eq(ChatMsg::getSignFlag, MsgSignFlagEnum.signed.type)
                .orderBy(true, true, ChatMsg::getId);
        List<ChatMsg> chatMsgList = chatMsgDao.selectList(queryWrapper);
        PageInfo<ChatMsg> chatMsgPageInfo = new PageInfo<>(chatMsgList);
        return ApiResult.success(chatMsgPageInfo);
    }
}
